package net.url;

import java.io.IOException;
import java.io.InputStream;
import java.net.ContentHandler;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

public class TimeUrlConnection extends URLConnection
{
	private Socket connection = null;
	public final static int DEFAULT_PORT =17;
	public TimeUrlConnection(URL u)
	{
		super(u);
	}
	@Override
	public String getContentType()
	{
		return "application/x_time";
	}
	@Override
	public Object getContent() throws IOException
	{
		ContentHandler ch = new x_time();
		return ch.getContent(this);
	}
	@SuppressWarnings("rawtypes")
	@Override
	public Object getContent(Class[] classes) throws IOException
	{
		ContentHandler ch = new x_time();
		return ch.getContent(this, classes);
	}
	@Override
	public InputStream getInputStream() throws IOException
	{
		if(!connected)
		{
			this.connect();
		}
		return this.connection.getInputStream();
	}
	@Override
	public void connect() throws IOException
	{
		if(!connected)
		{
			int port = url.getPort();
			if(port<0)
			{
				port = DEFAULT_PORT;
			}
			this.connection = new Socket(url.getHost(), port);
			this.connected=true;
		}
	}
	
}
